define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'car_cate/index' + location.search,
                    add_url: 'car_cate/add?pid='+ Fast.api.query('pid') + '&type='+ Fast.api.query('type'),
                    edit_url: 'car_cate/edit?pid='+ Fast.api.query('pid') + '&type='+ Fast.api.query('type'),
                    del_url: 'car_cate/del',
                    multi_url: 'car_cate/multi',
                    table: 'car_cate',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'sort', title: __('Sort'),width:120,operate: false,
                            formatter:function(value,row,index){
                                return '<input type="number" value="'+value+'" class="form-control sort_goods" data-id="'+row.id+'" data-value="'+value+'" style="width: 90%;display: inline-block" min="0">' +
                                    '</a>';
                            }
                        },
                        {field: 'cate_name', title: __('Cate_name')},
                        // {field: 'type', title: __('Type'),searchList: Config.typelist, formatter: Table.api.formatter.label, sortable:true},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate, buttons: [
                                {
                                    name:'model',
                                    text: function (row) {
                                        return '其下车型(' + row.child_cate_count + ')';
                                    },
                                    title:__('其下车型'),
                                    classname:'btn btn-xs btn-info btn-dialog',
                                    url: function (row){
                                        return 'car_cate/index?pid='+row.id+'&type=3';
                                    },
                                    visible: function (row) {
                                        if(row.type == 1){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    }
                                },
                            ]}
                    ]
                ]
            });

            //自定义Tab筛选条件
            $('.panel-heading .nav-custom-condition a[data-toggle="tab"]', table.closest(".panel-intro")).on('shown.bs.tab', function (e) {
                var value = $(this).data("value");
                var options = table.bootstrapTable('getOptions');
                var queryParams = options.queryParams;
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    //这一行必须要存在,否则在点击下一页时会丢失搜索栏数据
                    params = queryParams(params);
                    var pid = Config.pid;
                    params.pid = pid;
                    //如果希望追加搜索条件,可使用
                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    var op = params.op ? JSON.parse(params.op) : {};
                    if (value) {
                        //这里可以自定义多个筛选条件
                        filter.type = value;
                        op.type = '=';
                        params.type = value;
                        options.extend.add_url = 'car_cate/add?pid='+ Fast.api.query('pid') + '&type='+ value;
                    }
                    // else {
                    //     //选全部时要移除相应的条件
                    //     delete filter.type;
                    //     delete op.type;
                    // }

                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);

                    //如果希望忽略搜索栏搜索条件,可使用
                    //params.filter = JSON.stringify(value?{admin_id: value}:{});
                    //params.op = JSON.stringify(value?{admin_id: '='}:{});

                    return params;
                };

                table.trigger("uncheckbox");
                table.bootstrapTable('refresh', {pageNumber: 1});
                return false;
            });

            //排序
            $(document).on('blur', '.sort_goods', function (){
                var cate_id = $(this).attr('data-id');
                var new_sort = $(this).val();
                var sort_value = $(this).attr('data-value');
                if(new_sort != sort_value){
                    $.ajax({
                        type:'post',
                        url: 'car_cate/sort',
                        data:{cate_id:cate_id,new_sort:new_sort},
                        dataType:'json',
                        success:function (res){
                            if(res.code == 1){
                                Toastr.success('排序成功');
                                table.bootstrapTable('refresh', {});
                            }else{
                                toastr.error('排序失败');
                            }
                        }
                    });
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $("#c-type").val(Fast.api.query('type'));
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});